package com.chaitin.xray.form;

import com.chaitin.xray.utils.StringUtil;
import com.intellij.uiDesigner.core.GridConstraints;
import com.intellij.uiDesigner.core.GridLayoutManager;
import org.yaml.snakeyaml.LoaderOptions;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.SafeConstructor;

import javax.swing.*;
import java.awt.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;

public class LookupConfigForm {
    public JPanel lookupConfigPanel;
    private JTextArea configTextArea;
    private JButton saveConfigButton;
    private JButton refreshConfigButton;
    private JScrollPane configTextPanel;
    private JPanel configButtonPanel;

    private void initLang() {
        if (MainForm.LANG == MainForm.CHINESE) {
            saveConfigButton.setText("保存配置文件");
            refreshConfigButton.setText("刷新最新配置文件");
        } else {
            saveConfigButton.setText("Save Config");
            refreshConfigButton.setText("Refresh Config");
        }
    }

    public LookupConfigForm() {

        initLang();

        configTextArea.setText(MainForm.configStr);
        configTextArea.setCaretPosition(0);

        refreshConfigButton.addActionListener(e -> {
            if (!StringUtil.notEmpty(MainForm.configPath)) {
                JOptionPane.showMessageDialog(this.lookupConfigPanel, "无法刷新");
                return;
            }
            try {
                byte[] data = Files.readAllBytes(Paths.get(MainForm.configPath));
                configTextArea.setText(null);
                configTextArea.setText(new String(data));
            } catch (Exception ex) {
                ex.printStackTrace();
            }
            JOptionPane.showMessageDialog(this.lookupConfigPanel, "已刷新配置");
        });

        saveConfigButton.addActionListener(e -> {
            if (!StringUtil.notEmpty(MainForm.configPath)) {
                JOptionPane.showMessageDialog(this.lookupConfigPanel, "无法保存");
                return;
            }
            try {
                Yaml yaml = new Yaml(new SafeConstructor(new LoaderOptions()));
                String newConfig = configTextArea.getText();
                MainForm.configObj = yaml.load(newConfig);

                MainForm.configStr = newConfig;

                MainForm.instance.reloadConfig(false, false);

                Files.write(Paths.get(MainForm.configPath),
                        newConfig.getBytes(StandardCharsets.UTF_8));
                JOptionPane.showMessageDialog(this.lookupConfigPanel, "已保存配置");
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(this.lookupConfigPanel, "您的配置有误");
                ex.printStackTrace();
            }
        });
    }

    {
// GUI initializer generated by IntelliJ IDEA GUI Designer
// >>> IMPORTANT!! <<<
// DO NOT EDIT OR ADD ANY CODE HERE!
        $$$setupUI$$$();
    }

    /**
     * Method generated by IntelliJ IDEA GUI Designer
     * >>> IMPORTANT!! <<<
     * DO NOT edit this method OR call it in your code!
     *
     * @noinspection ALL
     */
    private void $$$setupUI$$$() {
        lookupConfigPanel = new JPanel();
        lookupConfigPanel.setLayout(new GridLayoutManager(2, 1, new Insets(0, 0, 0, 0), -1, -1));
        lookupConfigPanel.setBackground(new Color(-12828863));
        configTextPanel = new JScrollPane();
        configTextPanel.setBackground(new Color(-12828863));
        lookupConfigPanel.add(configTextPanel, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_WANT_GROW, new Dimension(600, 500), new Dimension(600, 500), null, 0, false));
        configTextArea = new JTextArea();
        configTextArea.setBackground(new Color(-12828863));
        configTextArea.setForeground(new Color(-1));
        configTextPanel.setViewportView(configTextArea);
        configButtonPanel = new JPanel();
        configButtonPanel.setLayout(new GridLayoutManager(1, 2, new Insets(0, 0, 0, 0), -1, -1));
        configButtonPanel.setBackground(new Color(-12828863));
        lookupConfigPanel.add(configButtonPanel, new GridConstraints(1, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false));
        saveConfigButton = new JButton();
        saveConfigButton.setText("保存配置文件");
        configButtonPanel.add(saveConfigButton, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        refreshConfigButton = new JButton();
        refreshConfigButton.setText("刷新最新配置文件");
        configButtonPanel.add(refreshConfigButton, new GridConstraints(0, 1, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
    }

    /**
     * @noinspection ALL
     */
    public JComponent $$$getRootComponent$$$() {
        return lookupConfigPanel;
    }

}
